package com.aierjun.ani.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by lenovo on 2016/10/19.
 */

public class Util {


    public static int getWidthPx(Context context) {
        int result;
        DisplayMetrics outMetrics = new DisplayMetrics();
        ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(outMetrics);
        result = outMetrics.widthPixels;
        return result;
    }

    public static int getHeightPx(Context context) {
        int result;
        DisplayMetrics outMetrics = new DisplayMetrics();
        ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(outMetrics);
        result = outMetrics.heightPixels;
        return result;
    }


    public static boolean isPhone(String phone) {
        return Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$").
                matcher(phone).matches();
    }



    public static int getAppVersionCode(Context context) {
        return getAppVersionCode(context, context.getPackageName());
    }


    public static int getAppVersionCode(Context context, String packageName) {
        if (!TextUtil.checkParams(packageName)) return -1;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            return pi == null ? -1 : pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return -1;
        }
    }


    public static String md5(String string) {
        byte[] hash;
        char hexDigits[] = {'0', '1', '2', '3', '4',
                '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));

            int j = hash.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = hash[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str).toLowerCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = new String(Base64.encode(b, Base64.DEFAULT));
        }
        s = s.replaceAll("\\n", "");
        return s;
    }


    public static String getDateStr(List<String> list) {
        if (list == null || list.size() < 3) {
            Calendar a = Calendar.getInstance();
            return a.get(Calendar.YEAR) + "-" + a.get(Calendar.MONTH) + "-" + a.get(Calendar.DAY_OF_MONTH);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            String str = list.get(i);
            if (str.length() == 1) {
                str = "0" + str;
            }
            switch (i) {
                case 0:
                    sb.append(str + "-");
                    break;
                case 1:
                    sb.append(str + "-");
                    break;
                case 2:
                    sb.append(str);
                    break;
            }
        }
        return sb.toString();
    }



    public static int getAge(List<String> list) {

        if (list == null || list.size() < 3) {

            return 1;
        }

        Calendar cal = Calendar.getInstance();

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        int yearBirth = Integer.parseInt(list.get(0));
        int monthBirth = Integer.parseInt(list.get(1));
        int dayOfMonthBirth = Integer.parseInt(list.get(2));
        //当前年份与出生年份相减，初步计算年龄
        int age = yearNow - yearBirth;
        //当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
        if (monthNow <= monthBirth) {
            //如果月份相等，在比较日期，如果当前日，小于出生日，也减1，表示不满多少周岁
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }

        return age;
    }
}
